package com.jd.tuning.jvm.threadpool;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


/**
 *  * 线程优化
 *  * 赖人工具
 *  * 原文：https://www.javacodegeeks.com/2012/03/threading-stories-about-robust-thread.html
 */
public class MyPoolSizeCalculator extends PoolSizeCalculator {

    public static void main(String[] args) {
        MyPoolSizeCalculator calculator = new MyPoolSizeCalculator();
        calculator.calculateBoundaries(
                //CPU目标利用率  U
                new BigDecimal(1.0),
                //blockingqueue占用的内存大小 单位buyte
                new BigDecimal(100000));
    }

    @Override
    protected long getCurrentThreadCPUTime() {
        //当前线程占用的总时间
        return ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
    }

    @Override
    protected Runnable creatTask() {
        return new AsynchronousTask();
    }

    @Override
    protected BlockingQueue createWorkQueue() {
        return new LinkedBlockingQueue<>();
    }

}

class  AsynchronousTask implements  Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}